//
//  WelcomeViewController.swift
//  iOS07-Weibo-LPK
//
//  Created by 罗培克 on 6/26/15.
//  Copyright © 2015 lpk. All rights reserved.
//

import UIKit
import SDWebImage

class WelcomeViewController: UIViewController {

    // 头像底部约束
    var bottomConstraint: NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        if let urlString = sharedUserAccount?.avatar_large {
            iconView.sd_setImageWithURL(NSURL(string: urlString))
        }
    }

    override func loadView() {
        view = UIView()
        
        let backImage = UIImageView(image: UIImage(named: "ad_background"))
        view.addSubview(backImage)
        view.addSubview(iconView)
        view.addSubview(titleLabel)
        
        backImage.setTranslatesAutoresizingMaskIntoConstraints(false)
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[subV]|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subV": backImage]))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[subV]|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subV": backImage]))
        
        iconView.setTranslatesAutoresizingMaskIntoConstraints(false)
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        bottomConstraint = NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: -160)
        view.addConstraint(bottomConstraint!)
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[subV(85)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subV" : iconView]))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[subV(85)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subV": iconView]))
        
        titleLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
        view.addConstraint(NSLayoutConstraint(item: titleLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0.0))
        view.addConstraint(NSLayoutConstraint(item: titleLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 8.0))
        
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
         bottomConstraint?.constant = -UIScreen.mainScreen().bounds.height + 200
        UIView.animateWithDuration(1.6, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(0.8, animations: { () -> Void in
                    self.titleLabel.alpha = 1.0
                    }, completion:{ _ in
                        NSNotificationCenter.defaultCenter().postNotificationName(LPKSwitchRootVCNotification, object: true)
                })
        }
    }
    
    // MARK: - 懒加载
    lazy var iconView: UIImageView = {
        let iconV = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        iconV.layer.masksToBounds = true
        iconV.layer.cornerRadius = 42.5
        
        return iconV
    }()
    
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        
        label.text = "欢迎回来"
        label.sizeToFit()
        label.alpha = 0.0
        
        return label
    }()

}
